package com.auto.autoapp.task

import com.auto.autoapp.constants.Dy
import com.auto.autoapp.step.toDyIndex
import com.auto.autoapp.util.SizeUtil
import com.auto.autoapp.util.addLog
import com.auto.autoapp.util.clickOrTap
import com.ven.assists.AssistsCore
import com.ven.assists.AssistsCore.getBoundsInScreen
import com.ven.assists.AssistsCore.setNodeText
import com.ven.assists.stepper.Step
import com.ven.assists.stepper.StepCollector
import com.ven.assists.stepper.StepImpl
import kotlinx.coroutines.delay

/**
 * 搜索任务
 */
class SearchTask : StepImpl() {
    override fun onImpl(collector: StepCollector) {
        collector.next(1) {
            return@next toDyIndex()
        }.next(2) {

            var result = Dy.ID_INDEX_SEARCH_NODE.clickOrTap()
            if (result.isSuccess) {
                addLog("点击搜索框")
                return@next Step.get(3)
            } else {
                addLog("未找到搜索按钮，请重新执行")
                return@next Step.none
            }

        }.next(3) {
            val searchInfo = collector.get(1).data.toString()
            var editText = AssistsCore.findById(Dy.ID_SEARCH_INPUT).firstOrNull()
            if (editText != null) {
                addLog("找到搜索输入框，输入内容: $searchInfo")
                editText.setNodeText(searchInfo)
                delay(1000)
                addLog("点击搜索按钮")
                Dy.SEARCH_BTN.clickOrTap()
            }
            return@next Step.none
        }
    }
}